# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import sys
import json
import codecs
import datetime
import time
from openpyxl import Workbook
reload(sys)
import os
from openpyxl import load_workbook
sys.setdefaultencoding('utf-8')

class SHMZX_new_Pipeline(object):
    def __init__(self):
        if  os.path.exists('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'_huamuzixun.xlsx'):
            self.wb = load_workbook('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'_huamuzixun.xlsx')
            self.ws = self.wb.active
        else:
            self.wb = Workbook()
            self.ws = self.wb.active
            self.ws.append(['title','desc','tel','people','link','currentDate'])

        #self.ws.append(['title','desc','tel','people','link','currentDate'])

    def process_item(self, item, spider):
        if(spider.name!="5858168"):
            return item
        line = [item['title'],item['desc'],item['tel'],item['people'],item['link'],item['currentDate']]
        self.ws.append(line)
        self.wb.save('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'_huamuzixun.xlsx')
        return item

class SHMZX_2_Pipeline(object):
    def __init__(self):
        if  os.path.exists('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'_huamuzixun_companyInfo.xlsx'):
            self.wb = load_workbook('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'_huamuzixun_companyInfo.xlsx')
            self.ws = self.wb.active
        else:
            self.wb = Workbook()
            self.ws = self.wb.active
            self.ws.append(['title','people','tel','desc','link'])

    def process_item(self, item, spider):
        if(spider.name!="5858168_2"):
            return item
        line = [item['title'],item['people'],item['tel'],item['desc'],item['link']]
        self.ws.append(line)
        self.wb.save('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'_huamuzixun_companyInfo.xlsx')
        return item

class SMTX_2_Pipeline(object):
    def __init__(self):
        if  os.path.exists('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'_miaotianxia_companyInfo.xlsx'):
            self.wb = load_workbook('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'_miaotianxia_companyInfo.xlsx')
            self.ws = self.wb.active
        else:
            self.wb = Workbook()
            self.ws = self.wb.active
            self.ws.append(['people','tel','email','address','desc','link'])

    def process_item(self, item, spider):
        if(spider.name!="miaotianxia_2"):
            return item
        line = [item['people'],item['tel'],item['email'],item['address'],item['desc'],item['link']]
        self.ws.append(line)
        self.wb.save('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'_miaotianxia_companyInfo.xlsx')
        return item

class SMTXPipeline(object):
    def __init__(self):
        if  os.path.exists('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'_miaotianxia.xlsx'):
            self.wb = load_workbook('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'_miaotianxia.xlsx')
            self.ws = self.wb.active
        else:
            self.wb = Workbook()
            self.ws = self.wb.active
            self.ws.append(['title','desc','num','tel','people','email','link','pubTime','address'])

    def process_item(self, item, spider):
        if(spider.name!="miaotianxia"):
            return item
        line = [item['title'],item['desc'],item['num'],item['tel'],item['people'],item['email'],item['link'],item['pubTime'],item['address']]
        self.ws.append(line)
        self.wb.save('D:\\DATA\\'+ time.strftime("%Y-%m-%d", time.localtime())+'_miaotianxia.xlsx')
        return item

class S312greenPipeline(object):
    def __init__(self):
        if  os.path.exists('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'_miaotianxia.xlsx'):
            self.wb = load_workbook('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'_miaotianxia.xlsx')
            self.ws = self.wb.active
        else:
            self.wb = Workbook()
            self.ws = self.wb.active
            self.ws.append(['title','desc','tel','people','cname','link'])

    def process_item(self, item, spider):

        line = [item['title'],item['desc'],item['tel'],item['people'],item['cname'],item['link']]
        self.ws.append(line)
        self.wb.save('D:\\DATA\\'+ time.strftime("%Y-%m-%d", time.localtime())+'_321green.xlsx')
        return item

class yunlin356Pipeline(object):
    def __init__(self):
        if  os.path.exists('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'_miaotianxia.xlsx'):
            self.wb = load_workbook('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'_miaotianxia.xlsx')
            self.ws = self.wb.active
        else:
            self.wb = Workbook()
            self.ws = self.wb.active
            self.ws.append(['title','desc','tel','people','cname','link','kk'])

    def process_item(self, item, spider):
        if(spider.name !="yuanlin365"):
            return item
        line = [item['title'],item['desc'],item['tel'],item['people'],item['cname'],item['link'],item['kk']]
        self.ws.append(line)
        self.wb.save('D:\\DATA\\'+ time.strftime("%Y-%m-%d", time.localtime())+'_yuanlin365.xlsx')
        return item

class xbmiaomuPipeline(object):
    def __init__(self):
        self.file = codecs.open(time.strftime("%Y-%m-%d",time.localtime()) +'_xbmiaomu.json','wb',encoding="utf-8")

    def process_item(self, item, spider):
        if(spider.name!="xbmiaomu"):
            return item
        line = json.dumps(dict(item)) +','+ '\n'
        self.file.write(line.decode("unicode_escape"))
        return item


class S597mmPipeline(object):
    def __init__(self):
        if  os.path.exists('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'S597mm.xlsx'):
            self.wb = load_workbook('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'S597mm.xlsx')
            self.ws = self.wb.active
        else:
            self.wb = Workbook()
            self.ws = self.wb.active
            self.ws.append(['苗商','主营','联系人','电话','手机','qq','邮箱','传真','link'])

    def process_item(self, item, spider):
        line = [item['cname'],item['desc'],item['people'],item['tel'],item['mobilephone'],item['QQ'],item['email'],item['fax'],item['link']]
        self.ws.append(line)
        print ">>>>>>>>>>>>>> saving data to excel  >>>>>>>>>>>>>>"
        self.wb.save('D:\\DATA\\'+ time.strftime("%Y-%m-%d", time.localtime())+'S597mm.xlsx')
        return item

class cx987Pipeline(object):
    def __init__(self):
        if  os.path.exists('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'cx987.xlsx'):
            self.wb = load_workbook('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'cx987.xlsx')
            self.ws = self.wb.active
        else:
            self.wb = Workbook()
            self.ws = self.wb.active
            self.ws.append(['苗商','主营','联系人','电话','手机','qq','邮箱','传真','link'])

    def process_item(self, item, spider):
        line = [item['cname'],item['desc'],item['people'],item['tel'],item['mobilephone'],item['QQ'],item['email'],item['fax'],item['link']]
        self.ws.append(line)
        print ">>>>>>>>>>>>>> saving data to excel  >>>>>>>>>>>>>>"
        self.wb.save('D:\\DATA\\'+ time.strftime("%Y-%m-%d", time.localtime())+'cx987.xlsx')
        return item

class miaomuzhanPipeline(object):
    def __init__(self):
        if  os.path.exists('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'miaomuzhan.xlsx'):
            self.wb = load_workbook('D:\\DATA\\'+time.strftime("%Y-%m-%d", time.localtime())+'miaomuzhan.xlsx')
            self.ws = self.wb.active
        else:
            self.wb = Workbook()
            self.ws = self.wb.active
            self.ws.append(['苗商','主营','联系人','电话','手机','qq','邮箱','地址','link'])

    def process_item(self, item, spider):
        line = [item['cname'],item['desc'],item['people'],item['tel'],item['mobilephone'],item['QQ'],item['email'],item['address'],item['link']]
        self.ws.append(line)
        print ">>>>>>>>>>>>>> saving data to excel  >>>>>>>>>>>>>>"
        self.wb.save('D:\\DATA\\'+ time.strftime("%Y-%m-%d", time.localtime())+'miaomuzhan.xlsx')
        return item
